package com.styytech.dadabao.ui.more;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.CommonResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.utils.CommonUtils;

/**
 * Created by user on 2015/8/24.
 */
@ContentView(R.layout.activity_feed_back)
public class FeedBackActivity extends BaseActivity {
    //设置最大字数
    private int txt_max_num=150;
    @ViewInject(R.id.rlyt_Opinion)
    private RelativeLayout rlyt_Opinion;
    //意见反馈
    @ViewInject(R.id.edt_txt)
    private EditText edt_txt;

    //意见反馈字数
    @ViewInject(R.id.tv_number)
    private TextView tv_number;
    //名字
    @ViewInject(R.id.edt_name)
    private EditText edt_name;
    //电话或邮箱
    @ViewInject(R.id.edt_phone)
    private EditText edt_phone;
    //提交
    @ViewInject(R.id.btn_load_opinion)
    private Button btn_load_opinion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)rlyt_Opinion
                .getLayoutParams();
        setTopBar("意见反馈", View.VISIBLE,View.VISIBLE,"返回");
        lp.width = (int)(CommonUtils.getWindowsWidth(this)*0.95);
        lp.height = (int) (CommonUtils.getWindowsHight(this) * 0.35);
        rlyt_Opinion.setLayoutParams(lp);
        edt_txt.addTextChangedListener(mTextWatcher);
        edt_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
        tv_number.setText("0/"+txt_max_num);
    }
    @OnClick(R.id.btn_load_opinion)
    private void OnClick(View view){
        switch (view.getId()){
            case R.id.btn_load_opinion:
                if ( edt_txt.getText().toString().equals("")){
                    CommonUtils.showToastMsg(FeedBackActivity.this, "反馈内容不能为空");
                }else if(edt_name.getText().toString().equals("")){
                    CommonUtils.showToastMsg(FeedBackActivity.this, "姓名不能为空");
                } else if(edt_phone.getText().toString().equals("")){
                    CommonUtils.showToastMsg(FeedBackActivity.this, "电话或邮箱不能为空");
                } else{
                    loadOpinion();
                }
                break;
        }
    }

    private void loadOpinion(){
            String url = ConstantsServerUrl.getLoadOpinionUrl();
            RequestParams params = new RequestParams();
            params.put("name", edt_name.getText().toString());
            params.put("mobile", edt_phone.getText().toString());
            params.put("content", edt_txt.getText().toString());

            CommonResult logResult = new CommonResult(responseResult);
            try {
                new HttpRequest(this).doPost(logResult,url,params);
            } catch (ArgumentsException e) {
                Log.i("INFO", "1e:" + e);
                e.printStackTrace();
            } catch (RequestException e) {
                Log.i("INFO","2e:"+e);
                e.printStackTrace();
            }
    }


    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
            CommonUtils.showToastMsg(FeedBackActivity.this, "提交成功！");
            finish();
        }

        @Override
        public void executeError(String message, int errorCode) {
            CommonUtils.showToastMsg(FeedBackActivity.this, ""+message);
        }

        @Override
        public void HttpError(String message, int errorCode) {
            CommonUtils.showToastMsg(FeedBackActivity.this, ""+message);
        }
    };
    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;
        private  EditText editnameText;
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
            // mTextView.setText(s);//将输入的内容实时显示
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            editStart = edt_txt.getSelectionStart();
            editEnd = edt_txt.getSelectionEnd();
            if (temp.length() > txt_max_num) {
                Toast.makeText(FeedBackActivity.this, "你输入的字数已经达到最大数值！",
                        Toast.LENGTH_SHORT).show();
                s.delete(editStart - 1, editEnd);
                int tempSelection = editStart;
                edt_txt.setText(s);
                edt_txt.setSelection(100);
                edt_txt.setFocusable(true);
                edt_txt.setFocusableInTouchMode(true);
                edt_txt.requestFocus();
                edt_txt.findFocus();

            }else {
                tv_number.setText(temp.length()+"/"+txt_max_num);
            }
        }
    };
}
